﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;

namespace HYElectricSystemSetting
{
	public partial class ElectricSystemSettingForm : System.Windows.Forms.Form
	{
		public ElectricSystemSettingForm(Document doc)
		{
			this.InitializeComponent();
			base.IsMdiContainer = true;
			this.m_docment = doc;
		}

		private void ElectricSystemSettingForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.es = new ElectricSystemSettingData();
			foreach (object obj in base.Controls)
			{
				Control control = (Control)obj;
				try
				{
					((MdiClient)control).BackColor = this.BackColor;
				}
				catch (InvalidCastException)
				{
				}
			}
			this.wsf = new WireSizeForm();
			this.wsf.MdiParent = this;
			this.wsf.Parent = this.panel1;
			this.wsf.Show();
			this.etf = new EnvironmentTemperatureForm();
			this.etf.MdiParent = this;
			this.etf.Parent = this.panel1;
			this.etf.Show();
			this.mlf = new MoreLoopForm();
			this.mlf.MdiParent = this;
			this.mlf.Parent = this.panel1;
			this.mlf.Show();
			this.ps = new PipeSize();
			this.ps.MdiParent = this;
			this.ps.Parent = this.panel1;
			this.ps.Show();
			this.wtp = new WireThroughPipes();
			this.wtp.MdiParent = this;
			this.wtp.Parent = this.panel1;
			this.wtp.Show();
			this.treeView1.ExpandAll();
		}

		private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
		{
			bool flag = false;
			if (e.Node.GetNodeCount(true) > 0)
			{
				this.treeView1.SelectedNode = e.Node.FirstNode;
			}
			foreach (object obj in base.Controls)
			{
				Control control = (Control)obj;
				if (control.Tag != null && this.treeView1.SelectedNode.Text == control.Tag.ToString())
				{
					control.BringToFront();
					flag = true;
				}
			}
			if (!flag)
			{
				string text = this.treeView1.SelectedNode.Text;
				if (text == "导线尺寸")
				{
					if (this.wsf != null)
					{
						this.wsf.Close();
					}
					this.wsf = new WireSizeForm();
					this.wsf.MdiParent = this;
					this.wsf.Parent = this.panel1;
					this.wsf.Show();
					this.wsf.BringToFront();
					return;
				}
				if (text == "环境温度")
				{
					if (this.etf != null)
					{
						this.etf.Close();
					}
					this.etf = new EnvironmentTemperatureForm();
					this.etf.MdiParent = this;
					this.etf.Parent = this.panel1;
					this.etf.Show();
					this.etf.BringToFront();
					return;
				}
				if (text == "多回路")
				{
					if (this.mlf != null)
					{
						this.mlf.Close();
					}
					this.mlf = new MoreLoopForm();
					this.mlf.MdiParent = this;
					this.mlf.Parent = this.panel1;
					this.mlf.Show();
					this.mlf.BringToFront();
					return;
				}
				if (text == "线管尺寸")
				{
					if (this.ps != null)
					{
						this.ps.Close();
					}
					this.ps = new PipeSize();
					this.ps.MdiParent = this;
					this.ps.Parent = this.panel1;
					this.ps.Show();
					this.ps.BringToFront();
					return;
				}
				if (!(text == "导线穿管"))
				{
					return;
				}
				if (this.wtp != null)
				{
					this.wtp.Close();
				}
				this.wtp = new WireThroughPipes();
				this.wtp.MdiParent = this;
				this.wtp.Parent = this.panel1;
				this.wtp.Show();
				this.wtp.BringToFront();
			}
		}

		public void GetWireTypeAllSelect()
		{
			if (ElectricSystemSettingData.WireTypeDatas != null)
			{
				this.wsf.cmbWireType.Items.Clear();
				foreach (WireTypeData item in ElectricSystemSettingData.WireTypeDatas)
				{
					this.wsf.cmbWireType.Items.Add(item);
					this.wsf.cmbWireType.DisplayMember = "WireType";
				}
				if (this.wsf.cmbWireType.Items.Count > 0)
				{
					this.wsf.cmbWireType.SelectedIndex = 0;
				}
			}
		}

		public void GetConduitTypeAllSelect()
		{
			if (ElectricSystemSettingData.ConduitTypeDatas != null)
			{
				this.caf.cmbConduitType.Items.Clear();
				foreach (ConduitTypeSetClass conduitTypeSetClass in ElectricSystemSettingData.ConduitTypeDatas)
				{
					this.caf.cmbConduitType.Items.Add(conduitTypeSetClass.ConduitTypeSet);
					this.caf.cmbConduitType.DisplayMember = "ConduitTypeSet";
				}
				if (this.caf.cmbConduitType.Items.Count > 0)
				{
					this.caf.cmbConduitType.SelectedIndex = 0;
				}
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			List<string> wireType = this.es.GetWireType();
			ElectricSystemSettingData.AddWireType(this.m_docment, wireType);
			base.Hide();
			try
			{
				this.Save();
			}
			catch (Exception)
			{
				MessageBox.Show("提交发生异常！请检查录入的数据是否有重复。");
			}
			base.Close();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (WireSizeForm.wireSizeIsEdit || MoreLoopForm.moreLoopIsEdit || EnvironmentTemperatureForm.EnvIsEdit || MoreLoopForm.moreLoopIsEdit || WireThroughPipes.wireThroughIsEdit || PipeSize.pipeSize)
			{
				MessageBoxButtons buttons = MessageBoxButtons.YesNo;
				if (MessageBox.Show("数据有改动，是否保存？", "盈建科提示", buttons) == DialogResult.Yes)
				{
					try
					{
						base.Hide();
						this.Save();
					}
					catch (Exception)
					{
						MessageBox.Show("提交发生异常！请检查录入的数据是否有重复。");
					}
				}
			}
			WireSizeForm.wireSizeIsEdit = false;
			EnvironmentTemperatureForm.EnvIsEdit = false;
			PipeSize.pipeSize = false;
			MoreLoopForm.moreLoopIsEdit = false;
			WireThroughPipes.wireThroughIsEdit = false;
			base.Close();
		}

		public void Save()
		{
			if (WireSizeForm.wireSizeIsEdit)
			{
				this.es.LoggingDataWireType();
				this.es.LoggingWireSizeDatas();
				this.es.loggingEnvTemrature();
			}
			if (MoreLoopForm.moreLoopIsEdit)
			{
				this.es.loggingLoopCorrection();
			}
			if (PipeSize.pipeSize)
			{
				this.es.LoggingDataConduitType();
				this.es.loggingPipeSizeDatas();
			}
			if (WireThroughPipes.wireThroughIsEdit)
			{
				this.es.loggingWireThrougDatas();
			}
			WireSizeForm.wireSizeIsEdit = false;
			EnvironmentTemperatureForm.EnvIsEdit = false;
			PipeSize.pipeSize = false;
			MoreLoopForm.moreLoopIsEdit = false;
			WireThroughPipes.wireThroughIsEdit = false;
		}

		private Document m_docment;

		private WireSizeForm wsf;

		private EnvironmentTemperatureForm etf;

		private MoreLoopForm mlf;

		private PipeSize ps;

		private ConduitAndFamily caf;

		private WireThroughPipes wtp;

		private ElectricSystemSettingData es;
	}
}
